/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.util;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;
 

public class DateHelper {
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	private static SimpleDateFormat ssdf = new SimpleDateFormat("yyyyMMdd");
	private static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static SimpleDateFormat time = new SimpleDateFormat("hh:mm:ss");
	private static Logger log=Logger.getLogger(DateHelper.class);
	private static Date rightNow = null;
	public static String getSimpleDate(Date date){ 
		try{
			return sdf.format(date);
		}catch(Exception e){
			log.error(" 转换日期发生异常 "+date);
			return "";
		}
	}
	public static String getShortDate(Date date){ 
		try{
			return ssdf.format(date);
		}catch(Exception e){
			log.error(" 转换日期发生异常 "+date);
			return "";
		}
	}
	public static String getDateTime(Date date){ 
		try{
			return df.format(date);
		}catch(Exception e){
			log.error(" 转换日期发生异常 "+date);
			return "";
		}
	}
	
	public static String getTime(Date date){
		try{
			return time.format(date);
		}catch(Exception e){
			log.error(" 转换时间发生异常 "+date);
			return "";
		}
	}
	
	public static Date parseSimple(String strDate){
		ParsePosition pos = new ParsePosition(0);
        Date strtodate = sdf.parse(strDate, pos);
        return strtodate; 
	}
	public static Date parse(String strDate){
		ParsePosition pos = new ParsePosition(0);
        Date strtodate = df.parse(strDate, pos);
        return strtodate; 
	}
	public static int getYear(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		return cld.get(Calendar.YEAR);
	}
	public static String getSimpleDate(long date){
		Calendar cld = Calendar.getInstance();
		cld.setTimeInMillis(date);
		return getSimpleDate(cld.getTime());
	}

	public static int getMonth(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		return cld.get(Calendar.MONTH) + 1;
	}

	public static int getDay(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		return cld.get(Calendar.DAY_OF_MONTH);
	}

	public static int getHour(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		return cld.get(Calendar.HOUR_OF_DAY);
	}

	public static int getMinute(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		return cld.get(Calendar.MINUTE);
	}

	public static int getSecond(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		return cld.get(Calendar.SECOND);
	}
	public static Date getWorkDay(Date now, int delay) {
		int multi5 = delay / 5;
		int mod5 = delay % 5;
		int weekday = now.getDay();

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(now);
		if (delay >= 5) {
			calendar.add(Calendar.DAY_OF_YEAR, multi5 * 7 + mod5);
		} else {
			if ((weekday + delay) <= 5) //不到周五
				calendar.add(Calendar.DAY_OF_YEAR, delay);
			else {
				if (weekday == 6) //当前周六
					calendar.add(Calendar.DAY_OF_YEAR, delay + 1);
				else {
					calendar.add(Calendar.DAY_OF_YEAR, delay + 2);
				}
			}

		}
		//    	获得预定工作日的内容，如果属于工作日，则减一天
		for (int i = 0; i < DocContants.workdayList.size(); i++) {
			Date workdayDate = (Date) DocContants.workdayList.get(i);
			if (workdayDate.compareTo(now) > 0
					&& workdayDate.compareTo(calendar.getTime()) < 0)
				calendar.add(Calendar.DAY_OF_YEAR, -1);
		}
		//获得预定节假日的内容，如果属于节假日，则加一天
		for (int i = 0; i < DocContants.holidayList.size(); i++) {
			Date holidayDate = (Date) DocContants.holidayList.get(i);
			if (holidayDate.compareTo(now) > 0
					&& holidayDate.compareTo(calendar.getTime()) <= 0)
				calendar.add(Calendar.DAY_OF_YEAR, 1);
		}

		return calendar.getTime();
	}
	public static java.sql.Date changeToSqlDate(java.util.Date source) {
		java.sql.Date dest = null;
		dest = new java.sql.Date(source.getTime());
		return dest;
	}

	public static java.util.Date getSystemTime() {
		rightNow = new java.util.Date();
		return rightNow;
	}

	public static int getYearBySystemTime() {
		Calendar rightNow = Calendar.getInstance();
		return rightNow.get(Calendar.YEAR);
	}

	public static int getMonthBySystemTime() {
		Calendar rightNow = Calendar.getInstance();
		return rightNow.get(Calendar.MONTH) + 1;//因为月份从0开始
	}

	public static int getDayBySystemTime() {
		Calendar rightNow = Calendar.getInstance();
		return rightNow.get(Calendar.DAY_OF_MONTH);
	}

	public static int getHourBySystemTime() {
		Calendar rightNow = Calendar.getInstance();
		return rightNow.get(Calendar.HOUR_OF_DAY);
	}

	public static int getMinuteBySystemTime() {
		Calendar rightNow = Calendar.getInstance();
		return rightNow.get(Calendar.MINUTE);
	}
	public static Date convertSimpleDateFormat(String simpleDate) {
		Date _date = null;
		try {
			_date = sdf.parse(simpleDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return _date;
	}
}
